package main

import (
	"fmt"
	"strconv"
	"sync"
	"time"
)

var breakTag = false

//func main() {
//	ch := make(chan string, 10)
//	wgPd := new(sync.WaitGroup)
//	wgCd := new(sync.WaitGroup)
//	for i := 0; i < 3; i++ {
//		wgPd.Add(1)
//		go produce(i, wgPd, ch)
//	}
//
//	for i := 0; i < 2; i++ {
//		wgCd.Add(1)
//		go consume(wgCd, ch)
//	}
//	go setBreakTag()
//
//	wgPd.Wait()
//	close(ch)
//	wgCd.Wait()
//}

/**
生产者
*/
func produce(threadId int, wag *sync.WaitGroup, ch chan string) {
	count := 0
	for !breakTag {
		time.Sleep(1 * time.Second)
		count++
		data := strconv.Itoa(threadId) + "------" + strconv.Itoa(count)
		fmt.Printf("生产者，%s\n", data)
		ch <- data
	}
	wag.Done()
}

/**
消费者
*/
func consume(wag *sync.WaitGroup, ch chan string) {
	for data := range ch {
		time.Sleep(1 * time.Second)
		fmt.Printf("消费者，%s\n", data)
	}
	wag.Done()
}

/**
设置退出标识
*/
func setBreakTag() {
	time.Sleep(2 * time.Second)
	breakTag = true
}
